#备忘录模式

#一、备忘录模式应用场景
#1、需要保存和恢复数据的相关状态场景。如保存游戏状态的场景；撤销场景，如Ctrl - Z操作；事务回滚的应用。一般情况下事务回滚有两种方式：一是把从恢复点开始的操作都反向执行一遍；二是直接恢复到恢复点的各种状态。两种方式各有优缺点，要结合业务场景，决定使用哪种模式；2、副本监控场景。备忘录可以当作一个临时的副本监控，实现非实时和准实时的监控————————————————


import random
class GameCharacter():
    vitality = 0
    attack = 0
    defense = 0

    def displayState(self):
        print ('Current Values:')

        print ('Life:%d' % self.vitality)

        print ('Attack:%d' % self.attack)

        print ('Defence:%d' % self.defense)


    def initState(self, vitality, attack, defense):
        self.vitality = vitality
        self.attack = attack
        self.defense = defense

    def saveState(self):
        return Memento(self.vitality, self.attack, self.defense)

    def recoverState(self, memento):
        self.vitality = memento.vitality
        self.attack = memento.attack
        self.defense = memento.defense


class FightCharactor(GameCharacter):
    def fight(self):
        self.vitality -= random.randint(1, 10)


class Memento:
    vitality = 0
    attack = 0
    defense = 0

    def __init__(self, vitality, attack, defense):
        self.vitality = vitality
        self.attack = attack
        self.defense = defense


if __name__ == "__main__":
    game_chrctr = FightCharactor()
    game_chrctr.initState(100, 79, 60)
    game_chrctr.displayState()
    memento = game_chrctr.saveState()
    game_chrctr.fight()
    game_chrctr.displayState()
    game_chrctr.recoverState(memento)
    game_chrctr.displayState()
